﻿using System;
using System.Collections.Generic;
using System.Text;
using Acms.Bll;
using Telerik.Web.UI;

namespace Acms.AcmsControls.ContentControls.Admin
{
    public class RelatedPostsAdminEdit : BaseAdminEdit<RelatedPostsContentControlProperties>
    {
        RadTreeView tree;

        protected override void CreateChildControls()
        {
            tree = new Telerik.Web.UI.RadTreeView();
            tree.ID = "tree";
            tree.DataBinding += new EventHandler(tree_DataBinding);
            tree.NodeExpand += new RadTreeViewEventHandler(tree_NodeExpand);
            tree.CheckBoxes = true;
            this.Controls.Add(tree);
            base.CreateChildControls();
        }

        void tree_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            e.Node.ExpandMode = TreeNodeExpandMode.ClientSide;
            Guid postGuid = new Guid(e.Node.Value);
            List<Post> posts = Post.GetAllByParentGuid(SiteId, postGuid, SiteVersion);
            foreach (Post post in posts)
            {
                e.Node.Nodes.Add(CreateNode(post));
            }
        }

        Guid[] checkedGuids = null;

        void tree_DataBinding(object sender, EventArgs e)
        {
            checkedGuids = propeties.PostGuids;
            //get site root
            Post root = Post.GetTopLevelBySiteId(base.SiteId, base.SiteVersion);
            if (root != null)
            {
                RadTreeNode rootNode = CreateNode(root);
                rootNode.Checkable = false;
                rootNode.Expanded = true;
                rootNode.ExpandMode = TreeNodeExpandMode.ClientSide;
                tree.Nodes.Add(rootNode);
                
                //recursively bind children

                foreach (Post child in root.Children)
                {
                    RadTreeNode childNode = CreateNode(child);
                    rootNode.Nodes.Add(childNode);
                }

            }
        }

        private RadTreeNode CreateNode(Post p)
        {
            RadTreeNode node = new RadTreeNode();
            node.Checkable = true;
            foreach(Guid tmp in checkedGuids)
            {
                if(tmp == p.PostGuid)
                {
                    node.Checked = true;
                }
            }
            node.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
            node.Text = p.DisplayName;
            node.Value = p.PostGuid.ToString();
            return node;
        }

        protected override void SaveToProperties(RelatedPostsContentControlProperties properties, Acms.Bll.PostControl control)
        {
            if (properties != null)
            {
                List<Guid> guids = new List<Guid>();
                GetCheckedGuids(tree.Nodes[0], guids);
                foreach (Guid tmpGuid in guids)
                {
                    if (!properties.Contains(tmpGuid))
                    {
                        properties.AddPostGuid(tmpGuid);
                    }
                }

                foreach (Guid tmpGuid in properties.PostGuids)
                {
                    if (!guids.Contains(tmpGuid))
                    {
                        properties.Remove(tmpGuid);
                    }
                }

            }
        }

        private void GetCheckedGuids(RadTreeNode node, List<Guid> guids)
        {
            if (node.Checked)
            {
                guids.Add(new Guid(node.Value));
            }
            foreach (RadTreeNode child in node.Nodes)
            {
                GetCheckedGuids(child, guids);
            }
        }

        protected override string GetCssClass
        {
            get { return "relatedPosts"; }
        }

        RelatedPostsContentControlProperties propeties = new RelatedPostsContentControlProperties();

        protected override object SaveViewState()
        {
            ViewState["Guids"] = checkedGuids;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            if(ViewState["Guids"] is Guid[])
            {
                checkedGuids = (Guid[])ViewState["Guids"];
            }
        }
    }
}
